// pages/essentalInfo/into.js

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaIndex: 0,
    area: [2, 3, 4, 5],
    viewList: new Array(2),
    shareholderType: [{ id: 1, value: '个人' }, { id: 2, value: '机构' }],
    typeValue: ["个人", "个人", "个人", "个人", "个人"],
    avatarUrl: '',
    nickName: '',
    have_calculation:0,
    text:"",

    complany_name : "",
    project_name : "",
  },

  //股东数量选择器
  bindPickerChange: function (e) {

    this.setData({
      areaIndex: e.detail.value,
      viewList: new Array(this.data.area[e.detail.value])
    });

    // console.log(this.data.viewList)
  },

  //股东类型改变
  shareholderTypeChange: function (e) {

    var typeValue = this.data.typeValue;
    for (var i = 0; i < this.data.typeValue.length; i++) {

      if (i == Number(e.currentTarget.id)) {
        typeValue[i] = this.data.shareholderType[Number(e.detail.value)].value;
        this.setData({
          typeValue: typeValue,
        })
      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    if (wx.getStorageSync('userInfo')) {
      this.setData({
        avatarUrl: JSON.parse(wx.getStorageSync('userInfo')).avatarUrl,
        nickName: JSON.parse(wx.getStorageSync('userInfo')).nickName,
      })
    }
    //加载是否显示用户记录
    this.setData({
      have_calculation : app.globalData.have_calculation,
      viewList: new Array(2),
    });

    if (app.globalData.have_calculation == 1){
      this.setData({
        text:"报告记录"
      })
    }else{
      this.setData({
        text: "返回首页"
      })
    }
  
    if (app.globalData.shareholders_list.length > 0) {
      this.setData({
        viewList: app.globalData.shareholders_list,
      })
    }

    // this.initValidate();
    this.$wuxToptips = app.Wux().$wuxToptips
    // console.log()
  },
  showToptips(error) {
    const hideToptips = this.$wuxToptips.show({
      timer: 3000,
      text: error.msg || '请填写正确的字段',
      success: () => console.log('toptips', error)
    })
  },

  //需要判断空值的字段
  initValidate() {
    
  },

  chackProject_nameLength:function(e){
    var value = e.detail.value;
    var that = this;

    var realLength = 0,
      len = value.length,
      charCode = -1;

    var str = "";
    for (var i = 0; i < len; i++) {
      charCode = value.charCodeAt(i);
      console.log(value[i]);
      if (charCode >= 0 && charCode <= 128) {
        realLength += 1;
      }else{
        realLength += 2;
      }

      if (realLength <= 8){
        str += value[i]
      }
    }
    return str;
  },
  chackCompany_nameLength:function(e){
    var value = e.detail.value;
    var that = this;

    var realLength = 0,
      len = value.length,
      charCode = -1;

    var str = "";
    for (var i = 0; i < len; i++) {
      charCode = value.charCodeAt(i);
      // console.log(value[i]);
      if (charCode >= 0 && charCode <= 128) {
        realLength += 1;
      } else {
        realLength += 2;
      }

      if (realLength <= 28) {
        str += value[i]
      }
    }
    return str;
  },
  
  check_name:function(e){
    var value = e.detail.value;
    var that = this;

    var realLength = 0,
      len = value.length,
      charCode = -1;

    var str = "";
    for (var i = 0; i < len; i++) {
      charCode = value.charCodeAt(i);
      // console.log(value[i]);
      if (charCode >= 0 && charCode <= 128) {
        realLength += 1;
      } else {
        realLength += 2;
      }

      if (realLength <= 10) {
        str += value[i]
      }
    }
    return str;
  }, 

  //判断字符长度方法
  // GetLength:function (str) {

  //   var realLength = 0, 
  //       len = str.length, 
  //       charCode = -1;
  //   for (var i = 0; i < len; i++) {
  //     charCode = str.charCodeAt(i);
  //     if (charCode >= 0 && charCode <= 128) realLength += 1;
  //     else realLength += 2;
  //   }

  //   return realLength
  //   // if (realLength >= length) {

  //   //   return true;
  //   // }
  //   // return false;
  // },
  //提交 将数据缓存在app中
  submitForm: function (e) {
    //form表单
    var detailValue = e.detail.value;
    //验证空值
    var rules = {
      project_name: {
        required: true,
      }};
    var messages = {
      project_name:{
        required: "项目名称必填",
      }
    };

    for (var ki in detailValue) {
      //判断是否是股东名称字段
      if (ki.indexOf("shareholder") > -1) {
        rules[ki] = {
          required: true,
        };
        messages[ki] = {
          required: "股东" + Number(Number(ki.substr(-1)) + 1) +"名称必填",
        }
      }
    }
    this.WxValidate = app.WxValidate(rules, messages);
    //判断字段空值
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      this.showToptips(error)
      return false
    }
    //股东数量
    var areaIndex = this.data.area[Number(this.data.areaIndex)];

    var appStoage = {};//缓存进app的字段
    appStoage.shareholders_amount = areaIndex;//存入股东数量
    appStoage.company_name = e.detail.value.company_name;//存入公司名称
    appStoage.project_name = e.detail.value.project_name;//存入项目名称

    var shareholders_list = [];//股东列表
    //循环所有key-value
    for (var ki in detailValue) {

     //股东
      var shareholders = {};

      //判断是否是股东名称字段
      if (ki.indexOf("shareholder") > -1) {
        var shareholders_number = Number(ki.substr(-1))+1 ; //股东编号
        var shareholders_name = detailValue[ki];  //股东名称
        //存入值
        shareholders.shareholders_number = shareholders_number;
        shareholders.shareholders_name = shareholders_name;
        shareholders.shareholders_type = Number(detailValue["type" + Number(ki.substr(-1))]) + 1;
        shareholders_list.push(shareholders);
      } 
    }
    appStoage.shareholders_list = JSON.stringify(shareholders_list)

    //存入缓存
    app.globalData.shareholders_list = shareholders_list;

    app.globalData.entity = appStoage;
    app.globalData.entity.utoken = wx.getStorageSync("utoken");
    wx.navigateTo({
      url: '/pages/contribution/contribution',
    });
  },

  icon_redirectTo:function(){
    var that = this;
    if (that.data.have_calculation == 1){
      wx.redirectTo({
        url: '/pages/history/index',
      })
    }else{
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
  },

})